using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace Rj.PersonnelVerification.Shared
{
    public static class SharedAssembly
    {
        public static Assembly Asssembly => Assembly.GetExecutingAssembly();

        public static void AddShareModeTransient(this IServiceCollection services)
        {
            IEnumerable<Type> types = Asssembly
                    .GetTypes()
                    .Where(x =>
                        x.IsClass && x.Namespace != null && !x.IsAbstract && !x.IsInterface
                        && x.Namespace!.StartsWith("Rj.PersonnelVerification.Shared.Models", StringComparison.InvariantCultureIgnoreCase)
                    );

            foreach (Type? type in types)
            {
                if (services.All(x => x.ServiceType != type))
                {
                    _ = services.AddTransient(type);
                }
            }
        }
    }
}
